<style>
    @import './less/AccountDetails.less';
</style>

<template>
    <card>
        <p slot="title">
            <Icon type="compose"></Icon>
            基本信息
        </p>
        <Form :model="formItem" label-width="120">
            <Row type="flex">
                <Col span="6">
                    <FormItem label="账户名称：">
                        <Input v-model="formItem.accountName" size="large" placeholder="请填写内容...">
                            <Icon type="information-circled" slot="prepend"></Icon>
                        </Input>
                    </FormItem>
                </Col>
                <Col span="6">
                    <FormItem label="管理员名称：">
                        <Input v-model="formItem.userName" size="large" placeholder="请填写内容...">
                            <Icon type="information-circled" slot="prepend"></Icon>
                        </Input>
                    </FormItem>
                </Col>
                <Col span="6">
                    <FormItem label="管理员账号：">
                        <Input v-model="formItem.userLoginName" size="large" placeholder="请填写内容...">
                            <Icon type="information-circled" slot="prepend"></Icon>
                        </Input>
                    </FormItem>
                </Col>
                <Col span="6">
                    <FormItem label="账户类型：">
                        <Select v-model="formItem.accountType" size="large" style="width:100%">
                            <Option v-for="item in typeList" :value="item.value" :key="item.value">{{ item.label }}</Option>
                        </Select>
                    </FormItem>
                </Col>
            </Row>
            <Row type="flex">
                <Col span="6">
                    <FormItem label="管理员密码：">
                        <Input v-model="formItem.password" size="large" type="password" placeholder="请填写内容...">
                            <Icon type="ios-locked-outline" slot="prepend"></Icon>
                        </Input>
                    </FormItem>
                </Col>
                <Col span="6">
                    <FormItem label="再次输入密码：">
                        <Input v-model="formItem.rpassword" size="large" type="password" placeholder="请填写内容...">
                            <Icon type="ios-locked-outline" slot="prepend"></Icon>
                        </Input>
                    </FormItem>
                </Col>
            </Row>
            <Row type="flex">
                <Col span="24">
                    <FormItem label="组织机构：">
                        <Row type="flex" class="code-row-bg">
                            <Col v-for="items in organList" class="select-margin">
                                <Select clearable filterable @on-change="changeSelected">
                                    <Option v-if="items.length === 0" value="-1" key="-1">无匹配数据</Option>
                                    <Option v-for="item in items" :value="item.id" :key="item.id">{{ item.organName }}</Option>
                                </Select>
                            </Col>
                        </Row>
                    </FormItem>
                </Col>
            </Row>
            <Row type="flex">
                <Col span="24">
                    <FormItem>
                        <Button @click="save" size="large" type="primary">保存</Button>
                        <Button @click="close" size="large" type="ghost" style="margin-left: 8px">关闭</Button>
                    </FormItem>
                </Col>
            </Row>
        </Form>
    </card>
</template>

<script>
export default {
    data () {
        return {
            baseURL: '/user/account',
            spinShow: false,
            formItem: {
                accountName: '',
                userName: '',
                userLoginName: '',
                password: '',
                rpassword: '',
                accountType: '',
                organId: ''
            },
            typeList: [
                {
                    value: 0,
                    label: '普通账户'
                },
                {
                    value: 1,
                    label: '管理员账户'
                }
            ],
            organList: []
        }
    },
    methods: {
        changeSelected (value) {
            if (value) {
                let url = this.baseURL + '/getChildren/' + value
                this.$ajax.get(url).then(function (response) {
                    let data = response.data
                    if (data.code === 0) {
                        let organ = data.data
                        this.formItem.organId = organ.id
                        let index = organ.level + 1
                        this.organList.every(function (item, i) {
                            if (i >= index) {
                                item.splice(0,item.length)
                            }
                            return true
                        }, this)
                        let children = data.params.children
                        children.forEach(function (item) {
                            this.organList[index].push(item)
                        }, this);
                    }
                }.bind(this))
            }
        },
        init () {
            this.$bus.init(this.formItem)
            this.organList = []
            let url = this.baseURL + '/getOrgans'
            this.$ajax.get(url).then(function (response) {
                let data = response.data
                if (data.code === 0) {
                    this.organList = data.data
                }
            }.bind(this))
        },
        save () {
            this.spinShow = true
            let url = this.baseURL + '/saveOrUpdate'
            this.$ajax.post(url, this.formItem).then(function (response) {
                let data = response.data
                if (data.code === 0) {
                    setTimeout(function () {
                        this.init()
                        this.spinShow = false
                        this.$bus.alert('success', '账户创建成功', 3)
                    }.bind(this), 1000)
                } else {
                    this.error(data.msg)
                }
            }.bind(this))
        },
        close () {
            this.$bus.closePageToLink()
        }
    },
    mounted () {
        this.init()
    },
    watch: {
        '$route' (to, from) {
            if (to.name === 'system_account_add') {
                this.init()
            }
        }
    }
}
</script>

<style>

</style>
